// sysvals.cpp
// Customizer_Base
// Protea Customizer/Customizer Pro / Business Logic for managing system values
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "base_setup.h"

// SYSTEM_VALUES Subtransactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 3
//@ CHA System Value Verify
//@ COD SystemValueVerify
//@ TAB SYSTEM_VALUES Read
//@ GEN Use SystemValue Verify to verify a System Value record
void SystemValueVerify( XCBuffer* buf, verify_type verify_mode, XCTransactionVars* vars, XCBuffer* orig ) 
{
	TRANS_INFUNC( SystemValueVerify );

	//@ SUB ValidateName to check for a valid name
	ValidateName( FLD_FIELD_NAME, buf, ERR_SYSTEM_VALUE, vars );

	//@ If verifying an ADD transaction,
	if (verify_mode == VERIFY_ADD) {
		//@ GRS

		//@ SUB LookupNumberFromName to convert system value field name into a system value field number
		//@ ERR C (ALREADY_EXISTS) System Value already exists
		if ( LookupNumberFromName( vars, vars->buffer, buf, FLD_FIELD_NUMBER, FLD_FIELD_NAME, FMT_SYSTEM_VALUES ) )
			vars->Errors->Add( ERR_ALREADY_EXISTS, SEV_CRITICAL, FLD_FIELD_NAME, 1, ERR_SYSTEM_VALUE );

		//@ GRE
	} else {
		//@ GRS
		
		//@ SUB SystemValuesRetrieve to verify that we do have a system value with this name
		//@ ERR C (DOESNT_EXIST) System Value doesn't exist
		if ( ! LookupNumberFromName( vars, vars->buffer, buf, FLD_FIELD_NUMBER, FLD_FIELD_NAME, FMT_SYSTEM_VALUES ) || 
			!SystemValueRetrieve( buf, vars ))
			vars->Errors->Add( ERR_DOESNT_EXIST, SEV_CRITICAL, FLD_FIELD_NAME, 1, ERR_SYSTEM_VALUE );

		//@ GRE
	};

	//@ If verifying an ADD or CHANGE transaction,
	if ( verify_mode == VERIFY_ADD || verify_mode == VERIFY_CHANGE )
	{
		//@ GRS
		//@ UTL IsValidDataType to ensure that the system value data type is correct
		//@ If the system value data type is incorrect, then
		//@ ERR C (INV_TYPE_VALUE) Field Value isn't valid for the Field Data Type
		if ( ! IsValidDataType( buf, vars ) )
			vars->Errors->Add( ERR_INV_TYPE_VALUE, SEV_CRITICAL, FLD_FIELD_VALUE );
	};

	TRANS_OUTFUNC();
};


//@ SEC 3
//@ CHA System Value Delete All
//@ COD SystemValueDeleteAll
//@ TAB SYSTEM_VALUES Write
//@ GEN Use to delete all system values records
void SystemValueDeleteAll( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( SystemValueDeleteAll );

	XCCompare comp[2];
	XCSQLGen* query = vars->query;
	XCField* flist[2];
	XCBuffer* sysval = App->CreateBuffer( FMT_SYSTEM_VALUES, false );
	sysval->MoveField( buf, FLD_APPL_NBR );

	//@ Delete the literal definition record
	query->MakeDELETE( sysval );

	flist[0] = sysval->GetField( FLD_APPL_NBR );
	flist[1] = NULL;
	query->AddWHERE( flist, comp );

	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	sysval->Release();

	TRANS_OUTFUNC();
};


//@ SEC 3
//@ CHA System Values Change
//@ COD SystemValuesChange
//@ TAB SYSTEM_VALUES Write
//@ GEN Use SystemValuesChange to alter System Values records
void SystemValueChange( XCBuffer* input, XCBuffer* compare, XCTransactionVars* vars ) {
	XCSQLGen* query = vars->query;

	TRANS_INFUNC( SystemValueChange );

	//@ Issue update statement
	query->MakeFull( SQL_STMT_UPDATE, input, compare );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};


//@ SEC 3
//@ CHA System Values Retrieve
//@ COD SystemValuesRetrieve
//@ TAB SYSTEM_VALUES Read
//@ GEN Use SystemValuesRetrieve to retrieve System Values records
bool SystemValueRetrieve( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( SystemValueRetrieve );

	bool result;
	XCDBStatement* stmt = NULL;
	XCSQLGen* query = vars->query;

	try {
		XCCompare comptemp;
		query->MakeFull( SQL_STMT_SELECT, buf );
		stmt = vars->dbtxn->ExecuteSQL( query );
		result = (!stmt->Eof());
	} catch( ... ) {
		App->CleanupObjects( 1, &stmt );
		throw;
	};
	App->CleanupObjects( 1, &stmt );

	TRANS_OUTFUNCRET( result );
};
